#!/bin/bash

source /home/shell/util.sh

# 安装函数
function install () {

    if [ ! -f "/usr/bin/java" ]; then
        ln -s /opt/jdk/bin/java /usr/bin/java
    fi

    while [ true ]; do
        curl -sL https://hub.fastgit.org/apolloconfig/apollo/releases|grep -Po "apollo-.*\.zip"|perl -pe "s/.*\D*(\d\.\d\.\d).*/\1/g"|uniq
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
            printFail "请输入版本"
        else
            break;
        fi
    done

    requireMysqlCient
    requireJava

    mkdir -p /opt/apollo/adminservice
    mkdir -p /opt/apollo/configservice
    mkdir -p /opt/apollo/portal

    download /opt/apollo-adminservice-github.zip https://hub.fastgit.org/apolloconfig/apollo/releases/download/v${version}/apollo-adminservice-${version}-github.zip
    download /opt/apollo-configservice-github.zip https://hub.fastgit.org/apolloconfig/apollo/releases/download/v${version}/apollo-configservice-${version}-github.zip
    download /opt/apollo-portal-github.zip https://hub.fastgit.org/apolloconfig/apollo/releases/download/v${version}/apollo-portal-${version}-github.zip

    git clone https://hub.fastgit.org/apolloconfig/apollo.git /usr/local/src/apollo/
    cd /usr/local/src/apollo
    git checkout -b ${version} origin/${version}
    git branch -a

    unzip -d /opt/apollo/adminservice /opt/apollo-adminservice-github.zip
    unzip -d /opt/apollo/configservice /opt/apollo-configservice-github.zip
    unzip -d /opt/apollo/portal /opt/apollo-portal-github.zip

    rm -rf /opt/apollo-adminservice-github.zip
    rm -rf /opt/apollo-configservice-github.zip
    rm -rf /opt/apollo-portal-github.zip
}

# 配置函数
function config(){

    read -p "请输入mysql 主机[${host_addr}]：" db_host
    db_host=${db_host:-"${host_addr}"};

    read -p "请输入mysql 用户名[dev_user]：" db_user
    db_user=${db_user:-"dev_user"};

    read -p "请输入mysql ${db_user}的密码[123456]：" db_passwd
    db_passwd=${db_passwd:-"123456"};

cat<<EOF
环境列表：
1. dev 开发环境，用于开发者调试使用
2. fat 功能验收测试环境，用于软件测试者测试使用
3. uat 用户验收测试环境，用于生产环境下的软件测试者测试使用
4. pro 生产环境
EOF
    read -p "选择环境[dev]：" apollo_portal_envs
    apollo_portal_envs=${apollo_portal_envs:-"dev"};

expect <<EOF
    spawn mysql_config_editor set --login-path=apollo --host=${db_host} --user=${db_user} --password
    expect {
        "password" {send "${db_passwd}\n";exp_continue}
        "exists" {send "y\n";interact}
    }
EOF
mysql --login-path=apollo<<EOF
source /usr/local/src/apollo/scripts/sql/apolloconfigdb.sql;
source /usr/local/src/apollo/scripts/sql/apolloportaldb.sql;
EOF

    rm -rf /usr/local/src/apollo/
    sed -i "s/fill-in-the-correct-server/"${db_host}"/g" /opt/apollo/adminservice/config/application-github.properties
    sed -i "s/FillInCorrectUser/"${db_user}"/g" /opt/apollo/adminservice/config/application-github.properties
    sed -i "s/FillInCorrectPassword/"${db_passwd}"/g" /opt/apollo/adminservice/config/application-github.properties

    sed -i "s/fill-in-the-correct-server/"${db_host}"/g" /opt/apollo/configservice/config/application-github.properties
    sed -i "s/FillInCorrectUser/"${db_user}"/g" /opt/apollo/configservice/config/application-github.properties
    sed -i "s/FillInCorrectPassword/"${db_passwd}"/g" /opt/apollo/configservice/config/application-github.properties

    sed -i "s/fill-in-the-correct-server/"${db_host}"/g" /opt/apollo/portal/config/application-github.properties
    sed -i "s/FillInCorrectUser/"${db_user}"/g" /opt/apollo/portal/config/application-github.properties
    sed -i "s/FillInCorrectPassword/"${db_passwd}"/g" /opt/apollo/portal/config/application-github.properties

    case "${apollo_portal_envs}" in
        dev)
            sed -i "s/fill-in-dev-meta-server/"${host_addr}"/g" /opt/apollo/portal/config/apollo-env.properties
        ;;
        fat)
            sed -i "s/fill-in-fat-meta-server/"${host_addr}"/g" /opt/apollo/portal/config/apollo-env.properties
        ;;
        uat)
            sed -i "s/fill-in-uat-meta-server/"${host_addr}"/g" /opt/apollo/portal/config/apollo-env.properties
        ;;
        pro)
            sed -i "s/fill-in-pro-meta-server/"${host_addr}"/g" /opt/apollo/portal/config/apollo-env.properties
        ;;
        *)
            printFail "输入的环境参数'${apollo_portal_envs}'有误，使用默认dev环境。"
            sed -i "s/fill-in-dev-meta-server/"${host_addr}"/g" /opt/apollo/portal/config/apollo-env.properties
            apollo_portal_envs="dev";
        ;;
    esac

mysql --login-path=apollo<<EOF
UPDATE ApolloPortalDB.ServerConfig SET \`Value\`='${apollo_portal_envs}' WHERE \`Key\`='apollo.portal.envs';
UPDATE ApolloPortalDB.ServerConfig SET \`Value\`='[{"orgId":"tech-department","orgName":"技术部"}]' WHERE \`Key\`='organizations';
EOF
    
    sed -i "s/#export JAVA_OPTS=\"\$JAVA_OPTS -Dspring.profiles.active=github -server -XX:-ReduceInitialCardMarks\"/export JAVA_OPTS=\"\$JAVA_OPTS -Dspring.profiles.active=github -server -XX:-ReduceInitialCardMarks\"/g" /opt/apollo/configservice/scripts/startup.sh
    sed -i "s/#export JAVA_OPTS=\"\$JAVA_OPTS -Dspring.profiles.active=github -server -XX:-ReduceInitialCardMarks\"/export JAVA_OPTS=\"\$JAVA_OPTS -Dspring.profiles.active=github -server -XX:-ReduceInitialCardMarks\"/g" /opt/apollo/adminservice/scripts/startup.sh
    sed -i "s/#export JAVA_OPTS=\"\$JAVA_OPTS -Dspring.profiles.active=github -server -XX:-ReduceInitialCardMarks\"/export JAVA_OPTS=\"\$JAVA_OPTS -Dspring.profiles.active=github,auth -server -XX:-ReduceInitialCardMarks\"/g" /opt/apollo/portal/scripts/startup.sh

    # firewall-addport 8080
    firewall-addport 8070
}

function run () {

bash /home/shell/serviced-install.sh<<EOF
2


configservice
bash /opt/apollo/configservice/scripts/startup.sh


bash /opt/apollo/configservice/scripts/shutdown.sh

EOF

bash /home/shell/serviced-install.sh<<EOF
2


adminservice
bash /opt/apollo/adminservice/scripts/startup.sh


bash /opt/apollo/adminservice/scripts/shutdown.sh 

EOF

bash /home/shell/serviced-install.sh<<EOF
2


portal
bash /opt/apollo/portal/scripts/startup.sh


bash /opt/apollo/portal/scripts/shutdown.sh 

EOF

    printInfo "==================================apollo=================================="
    printInfo "portal web地址：http://${host_addr}:8070"
    printInfo "portal web用户名：apollo"
    printInfo "portal web密码：admin"
    printInfo "configservice日志目录：/opt/logs/100003171"
    printInfo "adminservice日志目录：/opt/logs/100003172"
    printInfo "portal日志目录：/opt/logs/100003173"
    printInfo "portal环境配置文件：/opt/apollo/portal/config/apollo-env.properties"
    printInfo "当前环境：$apollo_portal_envs"
    printInfo "==================================apollo=================================="
}

# 运行安装和配置
install && config && run